import axios from 'axios';

var _axios = axios.create({
    baseURL: process.env.VUE_APP_BASE_API
})

_axios.interceptors.request.use(
    (config) => {
        // config.headers.token = ;
        return config;
    }, (error) => {
        return Promise.reject(error);
    }
);

_axios.interceptors.response.use(
    (response) => {
        // 登录失败
        // if(response.data.retCode === "no_token"){
        // }
        return response;
    }, (error) => {
        let message = '请求错误'
        if (error.response) {
            switch (error.response.status) {
                case 400:
                    message = '请求错误'
                    break
                case 401:
                    message = '未授权，请登录'
                    // localStorage.clear();
                    break
                case 403:
                  message = '拒绝访问'
                    break
                case 404:
                  message = `请求地址出错`
                    break
                case 408:
                    message = '请求超时'
                   break
                case 500:
                    message = '服务器内部错误'
                    break
                case 501:
                    message = '服务未实现'
                    break
                case 502:
                    message = '网关错误'
                    break
                case 503:
                    message = '服务不可用'
                    break
                case 504:
                    message = '网关超时'
                    break
                case 505:
                    message = 'HTTP版本不受支持'
                    break
                default:
                    break
            }
            // `请求地址出错: ${error.response.config.url}`
        }
        return Promise.reject(error, message);
    }
);

export default _axios